﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Silverlight.Library.RichContentService;

namespace FRL.DebtChased.ClientEditor
{

	/// <summary>
	/// Declaration of ClientDetailsCtrl class
	/// Control for editing client details, including website and activity
	/// </summary>
	public partial class ClientDetailsCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Delegates and events

		public event EventHandler ClientNameChanged;

		/////////////////////////////////////////////////////////////
		// Construction

		public ClientDetailsCtrl()
		{

			// Load xaml resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void txtClientName_TextChanged(object sender, TextChangedEventArgs e)
		{

			// Inform parent
			if (ClientNameChanged != null)
				ClientNameChanged(sender, null);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public void PostSupportDataLoaded()
		{

			// Set controls - drpActivity
			drpActivity.StringList = CachedData.Singleton.ClientActivities;

		}

		public void Get(ClientTransport clientTransport, List<string> Errors)
		{

			// Populate the client object with details from the control
			// and find errors in the user input

			// Check controls
			string strClientName = txtClientName.Text.Trim();
			if (strClientName.Length < 2)
				Errors.Add("The client name must be at least 2 characters.");
			string strActivity = drpActivity.Text.Trim();
			if (strActivity.Length < 2)
				Errors.Add("The client activity must be at least 2 characters.");

			// Update the client object
			clientTransport.Name = strClientName;
			clientTransport.Website = txtWebsite.Text;
			clientTransport.Activity = strActivity;

			// Add the activity to the list
			if (
				(strActivity.Length > 0)
				&&
				(!CachedData.Singleton.ClientActivities.Contains(strActivity))
			)
				CachedData.Singleton.ClientActivities.Add(strActivity);

		}

		public void Set(ClientTransport clientTransport)
		{

			// Set controls
			txtClientName.Text = clientTransport.Name ?? string.Empty;
			txtWebsite.Text = clientTransport.Website ?? string.Empty;
			drpActivity.Text = clientTransport.Activity ?? string.Empty;

		}

	}

}
